home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / filesyst / ncpfs / ipxripd-.000 / ipxripd- / ipxripd / ipxrip.c < prev    next >
C/C++ Source or Header  |  1996-02-08  |  4KB  |  205 lines

  1. /*
  2.     IPX support library - RIP
  3.  
  4.     Copyright (C) 1994, 1995  Ales Dryak <e-mail: A.Dryak@sh.cvut.cz>
  5.     Copyright (C) 1996, Volker Lendecke <lendecke@namu01.gwdg.de>
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. */
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include "ipxrip.h"
  25. #include "ipxd.h"
  26.  
  27. int
  28. ipx_rip_size(int n)
  29. {
  30.     if (n>IPX_RIP_MAX_ENTRIES)
  31.     {
  32.         return 0;
  33.     }
  34.     return 2 + n * sizeof(struct rip_entry);
  35. }
  36.  
  37. static int
  38. ipx_rip_sendto(int sock, struct rip_packet *buffer, int size,
  39.            struct sockaddr_ipx *daddr)
  40.     if (ipx_rip_output_func!=NULL)
  41.     {
  42.         return ipx_rip_output_func(sock, buffer, size, daddr);
  43.     }
  44.     return 0;
  45. }
  46.  
  47. int
  48. ipx_rip_output_init(struct rip_output *out, IPXNet net)
  49. {
  50.     out->entries=0;
  51.     out->send_error = 0;
  52.     out->buffer.operation=htons(IPX_RIP_OP_REQUEST);
  53.     out->dest_addr.sipx_family=AF_IPX;
  54.     out->dest_addr.sipx_type=IPX_RIP_PTYPE;
  55.     out->dest_addr.sipx_network=htonl(net);
  56.     return 0;
  57. }
  58.  
  59. void
  60. ipx_rip_output_flush(struct rip_output *out)
  61. {
  62.     if (out->entries==0)
  63.     {
  64.         return;
  65.     }
  66.     if (out->send_error != 0)
  67.     {
  68.         out->entries = 0;
  69.         return;
  70.     }
  71.     if (ipx_rip_sendto(out->sk, &(out->buffer), ipx_rip_size(out->entries),
  72.                &(out->dest_addr)) < 0)
  73.     {
  74.         out->send_error = 1;
  75.         check_request = 1;
  76.     }
  77.     out->entries=0;
  78. }
  79.  
  80. void
  81. ipx_rip_output_request(struct rip_output *out, IPXNet net)
  82. {
  83.     struct rip_entry *re;
  84.  
  85.     if (out->entries>=IPX_RIP_MAX_ENTRIES)
  86.     {
  87.         ipx_rip_output_flush(out);
  88.     }
  89.  
  90.     if (out->buffer.operation!=htons(IPX_RIP_OP_REQUEST))
  91.     {
  92.         ipx_rip_output_flush(out);
  93.         out->buffer.operation=htons(IPX_RIP_OP_REQUEST);
  94.     }
  95.  
  96.     re = &(out->buffer.rip_entries[out->entries]);
  97.     re->network = htonl(net);
  98.     re->hops    = 0;
  99.     re->ticks   = 0;
  100.     out->entries += 1;
  101. }
  102.  
  103. void
  104. ipx_rip_output_response(struct rip_output *out, IPXNet net, hop_t hops,
  105.             tick_t ticks, int down_allow)
  106. {
  107.     struct rip_entry* re;
  108.  
  109.     if (hops>=IPX_RIP_NET_DOWN && !down_allow)
  110.     {
  111.         return;
  112.     }
  113.  
  114.     if (out->entries>=IPX_RIP_MAX_ENTRIES)
  115.     {
  116.         ipx_rip_output_flush(out);
  117.     }
  118.  
  119.     if (out->buffer.operation!=htons(IPX_RIP_OP_RESPONSE))
  120.     {
  121.         ipx_rip_output_flush(out);
  122.         out->buffer.operation=htons(IPX_RIP_OP_RESPONSE);
  123.     }
  124.  
  125.     if (passive != 0)
  126.     {
  127.         return;
  128.     }
  129.  
  130.     re = &(out->buffer.rip_entries[out->entries]);
  131.     re->network = htonl(net);
  132.     re->hops    = htons(hops);
  133.     re->ticks   = htons(ticks);
  134.     out->entries += 1;
  135. }
  136.  
  137. void
  138. ipx_rip_output_set_destination(struct rip_output *out, IPXNode node,
  139.                    IPXPort port)
  140. {
  141.     ipx_rip_output_flush(out);
  142.     ipx_assign_node(out->dest_addr.sipx_node,node);
  143.     out->dest_addr.sipx_port=htons(port);
  144. }
  145.  
  146. void
  147. ipx_rip_dump(struct rip_packet *pkt, int len)
  148. {
  149.     ipx_rip_fdump(stdout, pkt, len);
  150. }
  151.  
  152. void
  153. ipx_rip_fdump(FILE *file, struct rip_packet *pkt, int len)
  154. {
  155.     struct rip_entry *re=pkt->rip_entries;
  156.     int ent = (len-2) / sizeof(struct rip_entry);
  157.  
  158.     if (len<2)
  159.     {
  160.         return;
  161.     }
  162.  
  163.     fprintf(file,"Operation: %i size: %i ", ntohs(pkt->operation),len);
  164.  
  165.     switch (ntohs(pkt->operation))
  166.     {
  167.     case IPX_RIP_OP_REQUEST:
  168.         fprintf(file,"(Request)\n");
  169.         if (len != ipx_rip_size(ent))
  170.         {
  171.             fprintf(file,"Warning: Bad RIP size (not 8*n+2)\n");
  172.         }
  173.         for(; ent > 0; ent -= 1)
  174.         {
  175.             fprintf(file,"RIP: Network: ");
  176.             ipx_fprint_network(file,ntohl(re->network));
  177.             fprintf(file,"\n");
  178.             re += 1;
  179.         }
  180.         break;
  181.     case IPX_RIP_OP_RESPONSE:
  182.         fprintf(file,"(Response)\n");
  183.         if (len != ipx_rip_size(ent))
  184.         {
  185.             fprintf(file,"Warning: Bad RIP size (not 8*n+2)\n");
  186.         }
  187.         for(; ent > 0; ent -= 1)
  188.         {
  189.             fprintf(file,"RIP: Network: ");
  190.             ipx_fprint_network(file,ntohl(re->network));
  191.             fprintf(file," HopCount: %u Ticks: %u\n",
  192.                 ntohs(re->hops),ntohs(re->ticks));
  193.             re += 1;
  194.         }
  195.         break;
  196.     default:
  197.         fprintf(file,"(Unknown)\n");
  198.         break;
  199.     }
  200. }
  201.  
  202. int (*ipx_rip_output_func)(int, void*, int, struct sockaddr_ipx*)=NULL;
  203.  
  204.